package lancooControl;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.presenter.PaperDef;

import static testPaperCTCS.GuessTestingPaperActivity.InforGuessTesting;
import static testPaperJBFX.BasicReviewPaperActivity.InforBasicReview;
import static testPaperNDFX.DifficultReviewPaperActivity.InforDifficultReview;
import static testPaperWrong.WrongQuesPaperActivity.InforWrongQues;

/**
 * Created by Administrator on 2017/4/14.
 */
public class SubmitDialog extends Dialog {

    private Context mContext;
    private int PaperFrom;
    private ClickListenerInterface clickListenerInterface;
    public static Button BtnEnsure;
    public static Button BtnCancle;
    public static TextView TvDialogTitle;
    public static LinearLayout LayoutButton;



    public interface ClickListenerInterface {
        public void doEnsure();
        public void doCancle();
    }

    public SubmitDialog(Context context,int paperFrom) {
        super(context, R.style.CustomDialog);
        this.mContext = context;
        this.PaperFrom = paperFrom;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        init();

        getWindow().setBackgroundDrawableResource(R.color.vifrification);
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.dialog_submit, null);
        setContentView(view);

        TvDialogTitle = (TextView) findViewById(R.id.textview_dialog_title);

        BtnEnsure = (Button) findViewById(R.id.button_ensure);
        BtnEnsure.setOnClickListener(new clickListener());

        BtnCancle = (Button) findViewById(R.id.button_cancle);
        BtnCancle.setOnClickListener(new clickListener());

        LayoutButton = (LinearLayout) findViewById(R.id.layout_button);

        setCanceledOnTouchOutside(false);

        int answeredItemCount = 0;
        int totalItemCount = 0;
        if(PaperFrom == PaperDef.FROM_BASE_REVIEW) {
            for (int i = 0; i < InforBasicReview.getPaperContent().size(); i++) {
                answeredItemCount = answeredItemCount + InforBasicReview.getPaperContent().get(i).getAnsweredItemCount();
                totalItemCount = totalItemCount + InforBasicReview.getPaperContent().get(i).getTotalItemCount();
            }

        }else if(PaperFrom == PaperDef.FROM_DIFFERENT_REVIEW){

            answeredItemCount = answeredItemCount + InforDifficultReview.getAnsweredItemCount();
            totalItemCount = totalItemCount + InforDifficultReview.getTotalItemCount();

        }else if(PaperFrom == PaperDef.FROM_GUESS_REVIEW){
            for (int i = 0; i < InforGuessTesting.getPaperContent().size(); i++) {
                answeredItemCount = answeredItemCount + InforGuessTesting.getPaperContent().get(i).getAnsweredItemCount();
                totalItemCount = totalItemCount + InforGuessTesting.getPaperContent().get(i).getTotalItemCount();
            }
        }else if(PaperFrom == PaperDef.FROM_BASE_REVIEW_WRONG||PaperFrom == PaperDef.FROM_DIFFERENT_REVIEW_WRONG){

            for (int i = 2; i < InforWrongQues.size(); i++) {
                answeredItemCount = answeredItemCount + InforWrongQues.get(i).getAnsweredItemCount();
                totalItemCount = totalItemCount + InforWrongQues.get(i).getTotalItemCount();
            }

        }
        int neverAnsweredCount = totalItemCount-answeredItemCount;
        setTitle(neverAnsweredCount);

    }

    public void setTitle(int neverAnswered){
        if(neverAnswered==0){
            TvDialogTitle.setText(Html.fromHtml("已经全部做完，是否提交？"));
        }else{
            TvDialogTitle.setText(Html.fromHtml("还有" + "<font color='#ff323d'><middle><middle>" + neverAnswered + "</middle></middle></font>"
                            + "道小题没有做完，确定提交？"));
        }
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }


    private class clickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int id = v.getId();
            switch (id) {
                case R.id.button_ensure:
                    System.out.println("------long-------cancle cancle  cancle----------------");
                    clickListenerInterface.doEnsure();
                    break;

                case R.id.button_cancle:
                    clickListenerInterface.doCancle();
                    break;

                default:
                    System.out.println("-----ooooooo11111defalt----------------");
                    break;
            }
        }

    };

}